import { Response, NextFunction } from 'express';
import { verifyToken } from './../utils';
// import HttpException from '../exceptions/HttpException';
// import { INTERNAL_SERVER_ERROR } from 'http-status-codes';

const loginMiddleware = async (req: any, res: Response, next: NextFunction) => {
  const token = req.headers['token'] as string;
  if (token) {
    const result = await verifyToken(token);
    if (result && result.id) {
      req['userId'] = result.id;
      next();
    } else {
      res.status(401).send({ error: 'token无效' });
    }
  } else {
    res.status(401).send({ error: '必须先登录' });
  }
}
export default loginMiddleware;